"""
@Name:numpy-practice1.py
@Auth:YJ
@Date:2023/11/7
"""
import numpy as np


def fill_ndarray(t1):
    # print(t1)
    for i in range(t1.shape[1]):  # 遍历每一列
        temp_col = t1[:, i]  # 当前的一列
        nan_num = np.count_nonzero(temp_col != temp_col)
        if nan_num != 0:
            temp_not_nan_col = temp_col[temp_col == temp_col]
            # 求当前列不为nan的平均值
            temp_not_nan_col_mean = temp_not_nan_col.mean()
            # 赋值给当前列为nan的位置
            temp_col[np.isnan(temp_col)] = temp_not_nan_col_mean
    return t1


if __name__ == '__main__':
    t1 = np.arange(12).reshape((3, 4)).astype("float")
    # 第二行第3、4列为nan
    t1[1, 2:] = np.nan
    print(t1)
    fill_ndarray(t1)
    print("*"*100)
    print(t1)
